package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.SystemdictionaryItem;
import cn.wolfcode.crm.mapper.SystemdictionaryItemMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.SystemdictionaryItemQuery;
import cn.wolfcode.crm.service.ISystemdictionaryItemService;
import cn.wolfcode.crm.shiro.MyRealm;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemdictionaryItemServiceImpl implements ISystemdictionaryItemService {
    @Autowired
    private SystemdictionaryItemMapper systemdictionaryItemMapper;
    @Autowired
    private MyRealm myRealm;


    @Override
    public void saveOrUpdate(SystemdictionaryItem entity) {
        if(entity.getId()!=null)
        {
            systemdictionaryItemMapper.updateByPrimaryKey(entity);
            //更新完成后清除缓存
            myRealm.clearCached();
        }else {
            systemdictionaryItemMapper.insert(entity);
        }
    }

    @Override
    public void delete(Long id) {
        systemdictionaryItemMapper.deleteByPrimaryKey(id);
        //删除后清除缓存
        myRealm.clearCached();
    }

    @Override
    public SystemdictionaryItem get(Long id) {
        return systemdictionaryItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SystemdictionaryItem> list() {
        return systemdictionaryItemMapper.selectAll();
    }

    @Override
    public PageInfo<SystemdictionaryItem> query(SystemdictionaryItemQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"sequence DESC");
        List<SystemdictionaryItem> list = systemdictionaryItemMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public List<SystemdictionaryItem> listItemByDicSn(String job) {
        return systemdictionaryItemMapper.listItemByDicSn(job);
    }
}
